//
//  MineViewController.m
//  NoneCar
//
//  Created by DongSen on 2018/7/10.
//  Copyright © 2018年 董森森. All rights reserved.
//
#import "LoginViewController.h"
#import "AppDelegate.h"
#import "LoginOutTableViewCell.h"
#import "APIManager+Login.h"
#import "MineTableViewCell.h"
#import "MineHeadView.h"
#import "MineViewController.h"
#import "ChangePassWordViewController.h"
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topHeight;
@property (strong, nonatomic)MineHeadView * headView;
@property (copy, nonatomic)NSArray * dataArray;

@property (copy, nonatomic)NSDictionary * infoDic;



@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.tableView.tableHeaderView = self.headView;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.topHeight.constant = -kSTATUSBARHEIGGT;
    self.navigationController.navigationBar.hidden = YES;
    [[UINavigationBar appearance] setTranslucent:NO];
    [self.tableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"LoginOutTableViewCell" bundle:nil] forCellReuseIdentifier:@"LoginOutTableViewCell"];
    self.dataArray = @[@[@"姓名",@"手机",@"归属机构",@"HR代码"],@[@"修改密码"],@[@"退出登录"]];
    self.headView.headNameLabel.text =[UserData sharedUserData].user.lastName;
    self.headView.nameLabel.text = [UserData sharedUserData].user.UserName;
    self.tableView.backgroundColor = BackGroundColor;
    [self getUserInfo];
//    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    // Do any additional setup after loading the view from its nib.
}
-(void)getUserInfo{
    NSDictionary * pars =@{@"UserId":@([UserData sharedUserData].user.UserId)};
    WeakSelf(self);
    [self showLoading];
    [[APIManager sharedNetManager] PostGetUserInfo:pars sucess:^(NSInteger code, NSDictionary *dict) {
        [weakSelf hidloading];
        if ([[dict objectForKey:@"Code"] integerValue]==1){
            weakSelf.infoDic = [dict objectForKey:@"Data"];
            [weakSelf.tableView reloadData];
        }
        else{
            [weakSelf showMessage:[dict objectForKey:@"Message"]];
        }
    } failure:^(NSInteger code, NSError *error) {
        [weakSelf hidloading];
        [weakSelf showNetError];
    }];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UINavigationBar appearance] setTranslucent:NO];
    self.navigationController.navigationBar.hidden = YES;    
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[UINavigationBar appearance] setTranslucent:YES];
}
-(MineHeadView *)headView{
    if (!_headView) {
        _headView = [MineHeadView loadView];
        _headView.headNameLabel.text = [UserData sharedUserData].user.lastName;
        _headView.headImageView.layer.masksToBounds = YES;
        _headView.headImageView.layer.cornerRadius = 28;
        _headView.headImageView.backgroundColor = STRCOLOR(@"#BED4FF");
        _headView.headImageView.layer.borderWidth =1;
        _headView.headImageView.layer.borderColor = STRCOLOR(@"#FFFFFFFF").CGColor;
    }
    return _headView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    if (indexPath.section==2) {
        LoginOutTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"LoginOutTableViewCell"];
        return cell;
    }
    else{
        MineTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"MineTableViewCell"];
        cell.titleLabel.text = self.dataArray[indexPath.section][indexPath.row];
        cell.valueLabel.text = @"";
        if (indexPath.section==0) {
            if (indexPath.row==0) {
                if (self.infoDic[@"UserName"]) {
                    cell.valueLabel.text = [NSString stringWithFormat:@"%@",self.infoDic[@"UserName"]];
                }
                else{
                    cell.valueLabel.text = @"";
                }
                cell.rightImageView.alpha = 0;
                cell.imageWidth.constant = 0;
            }
            if (indexPath.row==1) {
                if (self.infoDic[@"Mobile"]) {
                    cell.valueLabel.text = [NSString stringWithFormat:@"%@",self.infoDic[@"Mobile"]];
                }
                else{
                    cell.valueLabel.text = @"";
                }
                cell.rightImageView.alpha = 0;
                cell.imageWidth.constant = 0;
            }
            if (indexPath.row==2) {
                if (self.infoDic[@"OrganizationName"]) {
                    cell.valueLabel.text = [NSString stringWithFormat:@"%@",self.infoDic[@"OrganizationName"]];
                }
                else{
                    cell.valueLabel.text = @"";
                }
                cell.rightImageView.alpha = 0;
                cell.imageWidth.constant = 0;
            }
            if (indexPath.row==3) {
                if (self.infoDic[@"HRCode"]) {
                    cell.valueLabel.text = [NSString stringWithFormat:@"%@",self.infoDic[@"HRCode"]];
                }
                else{
                    cell.valueLabel.text = @"";
                }
                cell.rightImageView.alpha = 0;
                cell.imageWidth.constant = 0;
            }
        }
        if (indexPath.section==1){
            cell.valueLabel.text =@"";
            cell.rightImageView.alpha = 1;
            cell.imageWidth.constant = 12;
        }
        return cell;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section) {
        return 12;
    }
    return 200;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return self.headView;
    }
    else{
        return  [UIView new];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.dataArray[section] count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 52;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==2) {
        UIAlertController *alt = [UIAlertController alertControllerWithTitle:@"确定退出登录？" message:nil preferredStyle:UIAlertControllerStyleAlert];
        WeakSelf(self);
        UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf loginOut];
        }];
        UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alt addAction:cancelAction];
        [alt addAction:okAction];
        [self presentViewController:alt animated:YES completion:^{
            
        }];
    }
    if (indexPath.section==1) {
        ChangePassWordViewController * passworld = [ChangePassWordViewController new];
        passworld.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:passworld animated:YES];
    }
}
-(void)loginSucces{
    LoginViewController * login = [LoginViewController new];
    AppDelegate * delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    delegate.tabs.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    login.modalTransitionStyle =UIModalTransitionStyleCoverVertical;
    [UIView transitionWithView:self.view duration:1 options:UIModalTransitionStyleCoverVertical animations:^{
        [UIApplication sharedApplication].keyWindow.rootViewController = login;
    } completion:^(BOOL finished) {
        
    }];
}
-(void)loginOut{
    WeakSelf(self);
    [self showLoading];
    NSDictionary * pars =@{@"UserId":@([UserData sharedUserData].user.UserId)};
    [[APIManager sharedNetManager] PostLoginOut:pars sucess:^(NSInteger code, NSDictionary *dict) {
        [weakSelf hidloading];
        if ([[dict objectForKey:@"Code"] integerValue]==1){
            [[UserData sharedUserData] loginOut];
            [weakSelf loginSucces];
        }
        else{
            [weakSelf showMessage:[dict objectForKey:@"Message"]];
        }
    } failure:^(NSInteger code, NSError *error) {
        [weakSelf hidloading];
        [weakSelf showNetError];
    }];
}
#pragma mark --UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //图片高度
    CGFloat imageHeight = self.headView.frame.size.height-10;
    //图片宽度
    CGFloat imageWidth = kSCREEN_W;
    //图片上下偏移量
    CGFloat imageOffsetY = scrollView.contentOffset.y;
    //上移
    if (imageOffsetY < 0)
    {
        CGFloat totalOffset = imageHeight + ABS(imageOffsetY);
        CGFloat f = totalOffset / imageHeight;
        self.headView.headbackImageView.frame = CGRectMake(-(imageWidth * f - imageWidth) * 0.5, imageOffsetY, imageWidth * f, totalOffset);
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UIView * view = [UIView new];
//    view.backgroundColor = [UIColor redColor];
//    return view;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 100;
//}

@end
